\
\      End of run routine
\
L1:
       HALT
\
\      Print a number recursively
\
L2:
       STI  R3,4(R1)			 \ Save the return address
       LDI  8(R1),R15			 \ Copy arg to R15
       LDA  10(R0),R5			 \ Constant 10
       DIV  R15,R5               \ Non-zero if more than one digit
       BZE  L3				     \ Branch if only 1 digit
\
       STI  R5,12(R1)			 \ Save on stack for later
       STI  R5,24(R1)			 \ Copy to new stack frame
       LDA  L2,R2			     \ Address to call
       STI  R1,16(R1)			 \ Save stack pointer
       LDA  16(R1),R1			 \ Increment stack pointer
       BAL  R2,R3			     \ Call recursively
       LDA  10(R0),R15			 \ Constant 10 in R15
       LDI  8(R1),R5			 \ Original number
       LDI  12(R1),R6			 \ Divided number
       MUL  R6,R15			     \ Multiply back by 10 in R15
       SUB  R5,R15			     \ Subtract from original for digit
L3:
       LDA  48(R0),R5			 \ ASCII '0' in R5
       ADD  R5,R15			     \ Add to get desired digit in R15
       TRAP				         \ Print the digit
       LDI  4(R1),R2			 \ Return address in R2
       LDI  0(R1),R1			 \ Restore stack pointer
       BAL  R2,R3			     \ Return
\
\      Read a number
\ 
L8:
       STI  R3,4(R1)			 \ Save the return address
       LDR  R0,R4			     \ R4 = 0
       LDA  10(R0),R9            \ R9 = 10
L9:
       LDA  48(R0),R5			 \ ASCII '0' in R5
       READ				         \ Read the char in R15
       LDR  R15,R6			     \ Digit in R6
       LDA  47(R0),R7
       DIV  R7,R6			     \ R6 Zero if c>=48
       LDA  58(R0),R7
       DIV  R15,R7               \ R7 Zero if c< 58
       ADD  R6,R7			     \ R7 Zero if 48<= c <= 57
       BNZ  L7				     \ End if c is not a digit
       SUB  R15,R5               \ Value of the digit in R5
       MUL  R9,R4			     \ R10 = R4*10
       ADD  R5,R4                \ R10 = R4*10 + c
       BRA  L9				     \ Next digit
L7:
       LDI  4(R1),R2			 \ Return address in R2
       LDI  0(R1),R1			 \ Restore stack pointer
       BAL  R2,R3			     \ Return (the result is in R4)



\
\      Print a string
\
L4:
       STI  R3,4(R1)			 \ Save the return address
       LDI  8(R1),R5			 \ String pointer in R5
       LDA  16777216(R0),R7		 \ 256 cubed for shifting 24 bits right
L5:
       LDI  0(R5),R6			 \ Next char in top byte  of R6
       LDR  R7,R15			     \ Constant 256 cubed
       DIV  R6,R15			     \ Char in R15
       BZE  L6				     \ EOS branch to exit
       TRAP				         \ Print the char
       LDA  1(R5),R5			 \ Step to next char
       BRA  L5				     \ Round again
L6:
       LDI  4(R1),R2			 \ Return address in R2
       LDI  0(R1),R1			 \ Restore stack pointer
       BAL  R2,R3		  \ Return
\
